package utils

import (
	"path"
	"strings"
)

func JoinPaths(absolutePath, relativePath string) string {
	if relativePath == "" {
		return absolutePath
	}

	// 如果relativePath以斜线开始，移除开头的斜线
	if strings.HasPrefix(relativePath, "/") {
		relativePath = strings.TrimPrefix(relativePath, "/")
	}

	finalPath := path.Join(absolutePath, relativePath)

	// 检查原始相对路径是否以斜线结束，
	// 并且确认最终路径是否不以斜线结束。
	if strings.HasSuffix(relativePath, "/") && !strings.HasSuffix(finalPath, "/") {
		return finalPath + "/"
	}

	return finalPath
}
